﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using SharePoint.Contrib.Extensions;
using SharePoint.Contrib;
using System.Linq.Expressions;
using System.Xml.Linq;

namespace NUnit.SharePointContrib
{
    [TestFixture]
    public class CamlExpressionTreeTest
    {

        [SetUp]
        public void Init()
        {
        }

        [TearDown]
        public void Dispose()
        {
        }


        [Test]
        public void CanCreateWhere()
        {
            XElement xml = new XElement("Where");
            
            var where = CamlExpressionTree
                            .WithFilter()
                            .Where()
                            .Translate();

            Assert.That(where, Is.EqualTo(xml.ToString()));
        }


        [Test]
        public void CanTranslateStringProperty()
        {
            XElement xml = new XElement("Where", 
                                new XElement("Eq",
                                    new XElement("FieldRef", new XAttribute("Name", "StringProperty")),
                                    new XElement("Value", new XAttribute("Type", "Text"), "MyString")));

            var query = CamlExpressionTree
                            .WithFilter()
                            .Where()
                            .Translate(CreateExpression(i => i.StringProperty == "MyString"));
        }

        [Test]
        public void CanTranslateDateTime()
        {
            XElement xml = new XElement("Where",
                                new XElement("Eq",
                                    new XElement("FieldRef", new XAttribute("Name", "DateTimeProperty")),
                                    new XElement("Value", new XAttribute("Type", "DateTime"), new XElement("Today"))));

            var query = CamlExpressionTree
                            .WithFilter()
                            .Where()
                            .Translate(CreateExpression(i => i.DateTimeProperty == DateTime.Today));
        }

        [Test]
        public void CanTranslateWithAndJoin()
        {
            XElement xml = new XElement("Where",
                                new XElement("And",
                                new XElement("Eq",
                                    new XElement("FieldRef", new XAttribute("Name", "DateTimeProperty")),
                                    new XElement("Value", new XAttribute("Type", "DateTime"), new XElement("Today"))),
                                    new XElement("Eq",
                                    new XElement("FieldRef", new XAttribute("Name", "StringProperty")),
                                    new XElement("Value", new XAttribute("Type", "Text"), "MyString"))));

            var query = CamlExpressionTree
                            .WithFilter()
                            .Where()
                            .Translate(CreateExpression(i => i.DateTimeProperty == DateTime.Today && i.StringProperty == "MyString"));
        }

        [Test]
        public void CanTranslateWithOrJoin()
        {
            XElement xml = new XElement("Where",
                                new XElement("Or",
                                new XElement("Eq",
                                    new XElement("FieldRef", new XAttribute("Name", "DateTimeProperty")),
                                    new XElement("Value", new XAttribute("Type", "DateTime"), new XElement("Today"))),
                                    new XElement("Eq",
                                    new XElement("FieldRef", new XAttribute("Name", "StringProperty")),
                                    new XElement("Value", new XAttribute("Type", "Text"), "MyString"))));

            var query = CamlExpressionTree
                            .WithFilter()
                            .Where()
                            .Translate(CreateExpression(i => i.DateTimeProperty == DateTime.Today || i.StringProperty == "MyString"));
        }

        private Expression CreateExpression(Expression<Func<Item, bool>> expression)
        {
            return expression.Body;
        }

        private class Item
        {
            public string StringProperty { get; set; }
            public DateTime DateTimeProperty { get; set; }
        }

     
    }
}
